package br.com.clio.people;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import br.com.clio.buildings.Building;
import br.com.clio.city.City;
import br.com.clio.jobs.Job;
import br.com.clio.resources.Need;
import br.com.clio.resources.Resource;
import br.com.clio.resources.ResourceManager;
import br.com.clio.simulation.Simulation;
import br.com.clio.strata.Stratum;

public class Person {
	
	
	Simulation simu;
	City city;
	
	Job job;
	
	Building workplace;
	Building home;
	
	Stratum stratum;
	
	ResourceManager resources; //we'll use a manager to allow sub-typed needs
	
	FullfilmentManager fullfilment;//represents how well the person is satisfying its needs
	
	public void work(){//every day
		
		this.job.work();
		
	}
	
	public void consume(){//every day
		
		List<Need> needs = stratum.getNeeds();
		
		for (Need n : needs){
			Double success = resources.tryToConsume(n);
			fullfilment.updateFullfilment(n,success);
			}
		}
	
	public void enjoyLife(){
		
		//either each person gains ambient status (ie health from an aqueduct) here
		//or the ambient status forcefeed themselves into each person affected
		
		List<Entry<Resource,Double>> environment = home.getEnvironmentValues();
		
		for (Entry e : environment){
			resources.updateEnvironment(e);
		}
		
		
		
	}
	
	public void payTaxes(){
		
		//houseUpkeep?
		
		//stateTaxes
		
		//otherTaxes
		
	}
	
	public void evaluateNeeds(){//once in a while
		
		//Double projectedBudget = job.projectedBudget();
		
		
		
	}
	
	public void goShopping(){//you can only go shopping if you have paid your taxes!
		
		Double money = resources.getMoney();
		
		Double totalCost = 0.0;
		
		Double minPrice = city.getMarketManager().getPrice(stratum.getMinNeeds(),home);
		
		Double commonPrice = city.getMarketManager().getPrice(stratum.getCommonNeeds(),home);
		
		Double luxuryPrice = city.getMarketManager().getPrice(stratum.getLuxuryNeeds(),home);
		
		//if you have money for min+common, maybe you should stock some
		
		Double buyFactor = money>=minPrice?1.0:money/minPrice;
		
		totalCost+=city.getMarketManager().buyGoods(stratum.getMinNeeds(),home,buyFactor);
		
		buyFactor = (money-totalCost)>=commonPrice?1.0:(money-totalCost)/commonPrice;
		
		totalCost+=city.getMarketManager().buyGoods(stratum.getCommonNeeds(),home,buyFactor);
		
//		buyFactor = (money-totalCost)>=minPrice*1.2?1.2:(money-totalCost)/(minPrice*1.2);
//		
//		totalCost+=city.getMarketManager().buyGoods(stratum.getMinNeeds(),home,buyFactor);
		
		buyFactor = (money-totalCost)>=luxuryPrice?1.0:(money-totalCost)/luxuryPrice;
		
		totalCost+=city.getMarketManager().buyGoods(stratum.getLuxuryNeeds(),home,buyFactor);
		
		resources.spend(totalCost);
		
		
	}
	
	

}
